package de.pharmacontrol.psa.beans;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import de.pharmacontrol.pilot.entities.Line;
import de.pharmacontrol.psa.beans.interfaces.LineService;

@Stateless
public class LineServiceBean implements LineService {
	
	@PersistenceContext(unitName="pilot")
	protected EntityManager em;

	@Override
	public Line createLine(String name, String description) {
		Line line = new Line();
		line.setName(name);
		line.setDescription(description);
		em.persist(line);
		return line;
	}

	@Override
	public Line findLineByName(String name) {
		try {
			return em.createQuery("select l from Line l where l.name = :name", Line.class).setParameter("name", name).getSingleResult();
		}catch(NoResultException nre) {
			return null;
		}
		
	}

	@Override
	public Line updateLine(Line line) {
		return em.merge(line);
	}

}
